package com.itheima.user.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.user.pojo.MessageInfo;
import com.itheima.user.send.Sender;
import com.itheima.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private static List<MessageInfo> USER_MESSAGES = new ArrayList<>();

    @Autowired
    private Sender sender;

    @Override
    public List<MessageInfo> getHistoryMessage() {
        return USER_MESSAGES;
    }

    @Override
    public void sendMessage(MessageInfo messageInfo) throws JsonProcessingException {
        //使用消息中间件发送
        ObjectMapper objectMapper = new ObjectMapper();
        String message = objectMapper.writeValueAsString(messageInfo);
        sender.send(message);
    }

    @Override
    public void add(MessageInfo messageInfo){
        USER_MESSAGES.add(messageInfo);
    }

}
